package com.redis;

import com.redis.test01.TestRedisLock;
import com.redis.test03.TestMapper;
import com.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2019/9/9 20:21
 * 描述:
 */

@Controller
public class Test1Controller {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("toOrder")
    public String toOrder(){

        Object zhangsan1 = redisTemplate.opsForValue().get("zhangsan");

        System.out.println(zhangsan1);

        boolean zhangsan = RedisUtils.findToDelete(redisTemplate, "zhangsan");

        if(zhangsan){
            System.out.println("======OK====");
        }else{
            System.out.println("======NO=====");
        }

        return "order";
    }


    /**
     * 并发情况下的问题
     */
    @RequestMapping("bingfa")
    @ResponseBody
    public void forBingfa(){

        String zhangsan = redisTemplate.opsForValue().get("zhangsan");

        if(zhangsan!=null){
            Boolean zhangsan1 = redisTemplate.delete("zhangsan");
            if(zhangsan1){
                System.out.println("bingfa======OK====");
            }else{
                System.out.println("bingfa======NO=====");
            }
        }



    }


    /**
     * redis分布式锁的测试
     */
    @RequestMapping("testLock")
    public void testLock(){

        boolean lock = TestRedisLock.getLock("zhangsan", "zhangsan", redisTemplate, "5000");

        System.out.println(lock);

    }

    @Autowired
    TestMapper testMapper;

    @RequestMapping("mybatis")
    public void test(){

        Map<String, Object> byId = testMapper.getById(1L);
        System.out.println(byId);


    }

}
